// pages/my/my.js
// var user=require("../../data/data.js")
var my = require("../../utils/MyUtils.js")
var app = getApp();
var mta = require("../../libs/mta_analysis")
var log=require("../../utils/log")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: false,
    app: app,
    user: {}
  },
  globalData: {
    status1: true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    /**
     * 此处获取用户信息
     */
    var t = this
    var _TOKEN = wx.getStorageSync('_TOKEN')
    if (_TOKEN != "") {
      mta.Page.init()
      wx.request({
        url: app.globalData.url + '/oa-applet/user-info',
        header: {
          token: _TOKEN
        },
        data: {
          user_name: 'true',
          department_name: 'true',
          station_name: 'true',
          user_phone: 'true',
          first_department_name: 'true',
          user_email: 'true'
        },
        success: res => {
          /**
           * 获取用户信息
           */
          
          if (res.data.code == 0) {
            var user = res.data.data
            if (res.data.data.user_name.length >= 3) {
              user.nick_name = user.user_name.substr(1, user.user_name.length - 1)
            }else{
              user.nick_name = user.user_name
            }
            wx.setStorageSync('userInfo', user)
            t.setData({
              user: res.data.data
            })
          }else{
            t.setData({
              status: true
            })
          }
        },
      })
    } else {
      t.setData({
        status: true
      })
    }
  },
  onShow: function () {
    if(wx.getStorageSync('userInfo')){
      log.info(my.info(new Date(),wx.getStorageSync('userInfo').user_name," 进入了 ‘我的’页面"))
    }
  },
  /**
   * 跳转到显示个人信息
   */
  showMyInfo: function () {
    var that = this.data;
    if (that.status == false) {
      wx.navigateTo({
        //此处URL需要更改
        url: '../list/info/index?uid=' + this.data.user.id
      })
    } else {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000 //持续的时间
      })
    }
  },
  /**
   * 修改密码
   */
  updatePswd: function () {
    var that = this.data;
    if (that.status == false) {
      wx.navigateTo({
        //此处URL需要更改
        url: '../my/update/update'
      })
    } else {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000 //持续的时间
      })
    }
  },
  /**
   * 登出
   */
  loginOut: function () {
    var that = this;
    var _TOKEN = wx.getStorageSync('_TOKEN')
    if (!that.data.status) {
      if (_TOKEN != '') {
        my.alerts('确认退出登录？', res => {
          if (res.confirm) {
            wx.request({
              url: app.globalData.url + '/oa-applet/logout',
              method: 'POST',
              header: {
                token: _TOKEN
              },
              success: res => {
                if (res.data.code == 0) {
                  log.info(my.info(new Date(),wx.getStorageSync('userInfo').user_name,"退出了小程序"))
                  wx.removeStorageSync('_TOKEN', "")
                  wx.removeStorageSync('userInfo', "")
                  that.setData({
                    status: true,
                    user:{}
                  })
                  
                } else {
                  wx.showToast({
                    title: res.data.message,
                    icon: 'none'
                  })
                }
              }
            })
          }
        })
      } else {
        wx.showToast({
          title: '请先登录',
          icon: 'none',
          duration: 2000 //持续的时间        
        })
      }
    }
  },
  /**
   * 登录
   */
  login: function (e) {
    wx.navigateTo({
      url: '../login/login',
    })
  },
  //右上角分享功能
  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: '',
      path: '/pages/my/my',
      success: function (res) {
        // 转发成功

        that.shareClick();
        log.info()
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },

})